﻿using Microsoft.Extensions.DependencyInjection;
using System;

namespace WithDIContainer
{
    class Program
    {
        static void Main()
        {
            using (ServiceProvider container = RegisterServices())
            {
                //向容器请求HomeController对象
                var controller = container.GetRequiredService<HomeController>();
                string result = controller.Hello("Katharina");
                Console.WriteLine(result);
            }
        }

        static ServiceProvider RegisterServices()
        {
            //创建一个保管库
            var services = new ServiceCollection();
            //注册对应关系，IGreetingService和GreetingService
            //以便将来有用户请求IGreetingService，给出GreetingService单例对象（全应用唯一）
            services.AddSingleton<IGreetingService, GreetingService>();
            //注册HomeController
            //以便将来有用户请求HomeController，给出HomeController临时（瞬时）对象
            services.AddTransient<HomeController>();
            //创建保管员---》对外就是容器的代表
            return services.BuildServiceProvider();
        }
    }
}
